<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 15, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
define ( 'DS', DIRECTORY_SEPARATOR );

/**
 * The import utility.
 * When doing some import should use this class for 
 * avoiding duplicate include 1 class and reduce your code.
 *
 */
class AbstractRubikImporter {
	
	/**
	 * Check if the file is included --> don't do anything
	 * Else --> include this file.
	 *
	 * @param string $filePath filepath that we want to included
	 * @return boolean included or not
	 */
	static function checkAndInclude($filePath) {
		global $importFiles;
		if (empty ( $importFiles )) {
			$importFiles = array ();
		}
		
		$realPath = realpath ( $filePath );
		
		if (empty ( $importFiles [$realPath] )) {
			$importFiles [$realPath] = 1;
			if (empty ( $realPath )) {
				RError::raiseError ( 100, "Import: File <b>$filePath</b> not found." );
			}
			include ($realPath);
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Get the file path of a class
	 *
	 * @param string $clsName
	 */
	static function getFilePathOfClass($clsName) {
		RCoreImporter::cls('helper.RStringHelper');
		global $importFiles;
		$clsName .= '.class.php';
		foreach ($importFiles as $file=>$val) {
			if (RStringHelper::endWith($file, $clsName)) {
				return $file;
			}
		}
	}
	
	/**
	 * Import a class with the path is calculated from the current file that call this class
	 * <pre>
	 * If you are have some file like this
	 * 		UserDB.class.php
	 * 		DjetteDB.class.php
	 * 		[ext]
	 *			UserDBExt.class.php
	 *			GroupDBExt.class.php
	 * Then you want in DjettDB.class.php include the file GroupDBExt.class.php and UserDBExt.class.php
	 * --> you can write the code:
	 *             	AbstractRubikImporter::importClassFromThis('ext.GroupDBExt');
	 * 				AbstractRubikImporter::importClassFromThis('ext.UserDBExt');
	 * </pre>
	 * 
	 * @param string $callingFile the file path of the calling file. normally it should be __FILE__
	 * @param string $classPathFromThis
	 * @return boolean
	 */
	static function importClassFromThis($callingFile, $classPathFromThis) {
		return AbstractRubikImporter::checkAndInclude ( dirname ( $callingFile ) . DS . str_replace ( ".", DS, $classPathFromThis ) . ".class.php" );
	}
	
	/**
	 * Convert from class path format into php file.
	 * Ex: 	in: 	"dbo.database.Table"
	 * 		out: 	"dbo/database/Table.class.php"
	 *
	 * @param string $classPath Ex: "dbo.database.Table"
	 * @return string Ex: "dbo/database/Table.class.php"
	 */
	static protected function _extractClsPathToFile($classPath) {
		return str_replace ( ".", DS, $classPath ) . ".class.php";
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * The import utility.
 * When doing some import should use this class for 
 * avoiding duplicate include 1 class and reduce your code.
 *
 */
class RAppImporter extends AbstractRubikImporter {
	
	/**
	 * Import a file
	 * 
	 * @param string $filePath
	 */
	static function file($filePath) {
		return RAppImporter::checkAndInclude ( SYS_APP_PATH . $filePath );
	}
	
	/**
	 * Import a class
	 *
	 * @param string $classPath class path. it should be informat: "package.ClassName"
	 */
	static function cls($classPath) {
		$filePath = RAppImporter::_extractClsPathToFile ( $classPath );
		return RAppImporter::file ( $filePath );
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

define ( 'SYS_CLASSES_PATH', SYS_ROOT_PATH . "classes" . DS );

/**
 * The import utility.
 * When doing some import should use this class for 
 * avoiding duplicate include 1 class and reduce your code.
 *
 */
class RClassesImporter extends AbstractRubikImporter {
	
	/**
	 * Import a file
	 * 
	 * @param string $filePath
	 */
	static function file($filePath) {
		return RClassesImporter::checkAndInclude ( SYS_CLASSES_PATH . $filePath );
	}
	
	/**
	 * Import a class
	 *
	 * @param string $classPath class path. it should be informat: "package.ClassName"
	 */
	static function cls($classPath) {
		$filePath = RClassesImporter::_extractClsPathToFile ( $classPath );
		return RClassesImporter::file ( $filePath );
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

define ( 'SYS_CORE_PATH', SYS_ROOT_PATH . "core" . DS );

/**
 * The import utility.
 * When doing some import should use this class for 
 * avoiding duplicate include 1 class and reduce your code.
 *
 */
class RCoreImporter extends AbstractRubikImporter {
	
	/**
	 * Import sysProcess file
	 *
	 * @param string $filePath file from SysProcess
	 */
	static function sysProcessFile($filePath) {
		RCoreImporter::file ( "sysProcess/$filePath" );
	}
	
	/**
	 * Import a file
	 * 
	 * @param string $filePath
	 */
	static function file($filePath) {
		return RCoreImporter::checkAndInclude ( SYS_CORE_PATH . $filePath );
	}
	
	/**
	 * Import a class
	 *
	 * @param string $classPath class path. it should be informat: "package.ClassName"
	 */
	static function cls($classPath) {
		$filePath = RCoreImporter::_extractClsPathToFile ( $classPath );
		return RCoreImporter::file ( $filePath );
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

define ( 'SYS_EXT_PATH', SYS_ROOT_PATH . "ext" . DS );

/**
 * The import utility.
 * When doing some import should use this class for 
 * avoiding duplicate include 1 class and reduce your code.
 *
 */
class RExtImporter extends AbstractRubikImporter {
	
	/**
	 * Import a file
	 * 
	 * @param string $filePath
	 */
	static function file($filePath) {
		return RExtImporter::checkAndInclude ( SYS_EXT_PATH . $filePath );
	}
	
	/**
	 * Import a class
	 *
	 * @param string $classPath class path. it should be informat: "package.ClassName"
	 */
	static function cls($classPath) {
		$filePath = RExtImporter::_extractClsPathToFile ( $classPath );
		return RExtImporter::file ( $filePath );
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

define ( 'SYS_LIB_PATH', SYS_ROOT_PATH . "libraries" . DS );

/**
 * The import utility.
 * When doing some import should use this class for 
 * avoiding duplicate include 1 class and reduce your code.
 *
 */
class RLibImporter extends AbstractRubikImporter {
	
	/**
	 * Import a file
	 * 
	 * @param string $filePath
	 */
	static function file($filePath) {
		return RLibImporter::checkAndInclude ( SYS_LIB_PATH . $filePath );
	}
	
	/**
	 * Import a class
	 *
	 * @param string $classPath class path. it should be informat: "package.ClassName"
	 */
	static function cls($classPath) {
		$filePath = RLibImporter::_extractClsPathToFile ( $classPath );
		return RLibImporter::file ( $filePath );
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * The import utility.
 * When doing some import should use this class for 
 * avoiding duplicate include 1 class and reduce your code.
 *
 */
class RRootImporter extends AbstractRubikImporter {
	
	/**
	 * Import Configuration file
	 *
	 * @param string $filePath file from SysProcess
	 */
	static function configFile($filePath) {
		RRootImporter::file ( "config/$filePath" );
	}
	
	/**
	 * Import class inside configuartion folder
	 *
	 * @param string $filePath file from SysProcess
	 */
	static function configCls($classPath) {
		RRootImporter::cls( "config.{$classPath}" );
	}
	
	/**
	 * Import a file
	 * 
	 * @param string $filePath
	 */
	static function file($filePath) {
		return RRootImporter::checkAndInclude ( SYS_ROOT_PATH . $filePath );
	}
	
	/**
	 * Import a class
	 *
	 * @param string $classPath class path. it should be informat: "package.ClassName"
	 */
	static function cls($classPath) {
		$filePath = RRootImporter::_extractClsPathToFile ( $classPath );
		return RRootImporter::file ( $filePath );
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * The import utility.
 * When doing some import should use this class for 
 * avoiding duplicate include 1 class and reduce your code.
 *
 */
class RPluginImporter extends AbstractRubikImporter {
	
	/**
	 * Import a file
	 * 
	 * @param string $filePath
	 */
	static function file($filePath) {
		return RPluginImporter::checkAndInclude ( SYS_PLUGIN_PATH . $filePath );
	}
	
	/**
	 * Import a class
	 *
	 * @param string $classPath class path. it should be informat: "package.ClassName"
	 */
	static function cls($classPath) {
		$filePath = RPluginImporter::_extractClsPathToFile ( $classPath );
		return RPluginImporter::file ( $filePath );
	}
}

?>
